package com.jvm123.api.api.controller;

import com.jvm123.api.api.client.PostsClient;
import com.jvm123.api.api.entity.Post;
import com.jvm123.api.api.service.PostService;
import com.jvm123.api.util.Page;
import com.jvm123.meilisearch.json.SearchResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yawn http://jvm123.com
 * 2019/11/30 14:53
 */
@RestController
@RequestMapping("post")
public class PostsController {

    @Resource
    PostsClient postsClient;
    @Resource
    PostService postService;


    /**
     * 关键词搜索
     * @param wd 关键词
     */
    @GetMapping("s/{wd}")
    public List<Post> search(@PathVariable("wd") String wd) {
        SearchResult<Post> result = postService.search(wd);
        return result.getHits();
    }

    @GetMapping("{id}")
    public Post get(@PathVariable("id") Long id) {
        return postsClient.get(id);
    }

    @GetMapping("page")
    public Page<Post> page(int page, int size) {
        int offset = (page - 1) * size;
        List<Post> list = postService.list(offset, size);
        return new Page<>(page, size, list);
    }


}
